home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / editors / mutt / me2s_pl7.zoo / mu_edit2 / util / argh.c next >
Encoding:
C/C++ Source or Header  |  1993-07-05  |  2.2 KB  |  72 lines

  1. static char rcsid[] = "$Id: argh.c,v 1.1 1992/09/06 19:31:32 mike Exp $";
  2.  
  3. /* $Log: argh.c,v $
  4.  * Revision 1.1  1992/09/06  19:31:32  mike
  5.  * Initial revision
  6.  *
  7.  */
  8.  
  9. /*
  10.  * ARGH(): simular to GETOPT(3)
  11.  * C Durland 7/85    Public Domain
  12.  *
  13.  * argh(argc,argv,options)
  14.  *  options string is CASE SENSITIVE.  If option letter is followed
  15.  *    by a ':' it means option requires a word.
  16.  *  returns: -1 (error), 0 (done), 1 (OK), 2 (no '-' but a word)
  17.  *  argc, argv & options are unmodified by argh().
  18.  * Notes: It is expected that you will only process the command line
  19.  *    with this program ONCE! (unless you reset optind & optoff).
  20.  *    if 1: &':'||2 (optarg!='\0')
  21.  *    if -1: (optarg==?)
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include "const.h"
  27.  
  28.     /* the next 3 vars are to be used by the calling pgm */
  29. char *optarg,        /* pointer to word */
  30.      optltr;        /* option letter */
  31. int opterr = TRUE;    /* tell errors?    */
  32.  
  33.     /* Next 2 vars can be used to restart argh() for example if you have
  34.      *   a recursive main()
  35.      */
  36. int optind = 1,        /* index into argv */
  37.     optoff = 0;        /* offset into argv[optind] */
  38.  
  39. argh(argc,argv,options) int argc; char **argv, *options;
  40. {
  41.   char *option, *strchr();
  42.  
  43. loop:
  44.   if (optind>=argc) return 0;    /* no more args in arglist */
  45.   optarg = argv[optind];
  46.   if (optoff==0) /* new arg => check to see if an option */
  47.     if (optarg[0]=='-') optoff++;        /* skip to next char */
  48.     else { optind++; optltr = '?'; return 2; }    /* not option arg */
  49.   if ((optltr = optarg[optoff++]) =='\0')    /* try next arg */
  50.     { optind++; optoff = 0; goto loop; }
  51. /*  optltr = toupper(optltr); */
  52.   if ((option = strchr(options,optltr)) ==(char *)NULL)
  53.   {
  54.     if (opterr) fprintf(stderr,"unknown option: '%c'\n",optltr);
  55.     return -1;        /* not valid option */
  56.   }
  57.   if (option[1]==':')
  58.   {        /* word expected, check next char & skip to next arg */
  59.     optarg = argv[optind++] +optoff; optoff = 0;
  60.     if ( (*optarg)=='\0' )
  61.             /* end of arg so check for word in next arg */
  62.       if (optind>=argc)
  63.       {    /* no more arguments */
  64.         if (opterr)
  65.       fprintf(stderr,"'%c' option requires an argument\n",optltr);
  66.         return -1;
  67.       }
  68.       else optarg = argv[optind++];
  69.   }
  70.   return 1;
  71. }
  72.